<?php
namespace App\Services\IntegralOrder\Order;

use App\Models\IntegralOrder\Order;
use App\Models\IntegralOrder\PickUpGoods;
use Illuminate\Support\Facades\DB;
/**
 * 商品 逻辑层
 * Class OrderLogic
 * @package App\Services\Order\Order
 */
class OrderLogic 
{
    
    /*积分兑换商品*/
   /* public function integralExchange($data): array 
    {
        //1、积分商品数量-1
        $_descWhere['id'] = $data['goods_id'];
        $_descWhere['status'] = 1;
        $_descWhere['is_del'] = 0;
        $descRes = $this->model($this->goodsModel)->where($_descWhere)->setDec('number', $data['number']);
        if(!$descRes){ return verify_err(null, '减少商品数量失败'); }
        //2、形成订单记录
        $goodsInfo = $this->model($this->goodsModel)->where(['id'=>$data['goods_id']])->find();
        if(!$goodsInfo){ return verify_err(null, '该积分商品不存在'); }
        $addOrder['user_id'] = $data['user_id'];
        $addOrder['goods_id'] = $data['goods_id'];
        $addOrder['number'] = $data['number'];
        $addOrder['integral'] = $goodsInfo['integral'];
        $addOrder['order_code'] = $data['order_code'];
        $addOrder['freeze_status'] = 1;
        $addOrder['pay_time'] = NOW;
        $addOrder['create_time'] = NOW;
        $addOrder['update_time'] = NOW;
        try{
            $addOrderRes = $this->model($this->orderModel)->insertGetId($addOrder);
        }catch(\Exception $e){
            return server_err(null, $e->getMessage());
        }
        //3、添加提货信息
        $addPick['pick_up_code'] = $data['pick_up_code'];
        $addPick['order_id'] = $addOrderRes;
        $addPick['user_id'] = $data['user_id'];
        $addPick['name'] = $data['name'];
        $addPick['phone'] = $data['phone'];
        $addPick['address'] = $data['address'];
        $addPick['remark'] = $data['remark'];
        try{
            $addPickRes = $this->model($this->pickUpGoodsModel)->save($addPick);
        }catch(\Exception $e){
            return server_err(null, $e->getMessage());
        }
        return auto_return($addOrderRes);
    }*/

    /*内部获取积分订单列表 分页数据  order左链接goods表 左链接pick_up_goods表*/
    protected function _integralOrderList(array $data = [], array $_where): array 
    {
         $list = Order::leftJoin('integral_goods', 'integral_goods.id', '=', 'integral_order.goods_id')->leftJoin('integral_pick_up_goods','integral_pick_up_goods.order_id','=','integral_order.id')
            ->select('integral_order.*', 'integral_goods.title as goods_title','integral_pick_up_goods.pick_up_code','integral_pick_up_goods.name as pick_up_name','integral_pick_up_goods.phone as pick_up_phone','integral_pick_up_goods.address as pick_up_address','integral_pick_up_goods.remark as pick_up_remark','integral_pick_up_goods.is_pick','integral_pick_up_goods.pick_up_time','integral_pick_up_goods.logistics_company_code','integral_pick_up_goods.logistics_company_name','integral_pick_up_goods.waybill_number')
            ->where($_where)
            ->orderBy('integral_order.id', 'desc')
            ->paginate($data['listRows'], null, null, $data['page'])
            ->toArray();
        if($list['total'] > 0){
            foreach($list['data'] as $k=>&$v){
                if($v['freeze_status'] == 1){
                    if($v['transfer_status'] == 1){
                        $v['status_text'] = $v['is_pick'] == 1 ? '已收货' : '已发货';
                    }else{
                        $v['status_text'] = '待发货';
                    }
                }else{
                    $v['status_text'] = '未知状态';
                }
            }
        }
        return auto_return_page($list);
    }

    /*获取我的积分兑换订单*/
    /*public function integralOrderList($data): array 
    {
        $_where['order.user_id'] = $data['user_id'];
        $_where['order.is_del'] = 0;
        $data['keywords'] && $_where['goods.title'] = ['like', '%' . $data['keywords'] . '%'];
        return $this->_integralOrderList($data, $_where);
    }*/

    /*后台获取积分订单列表*/
    public function adminIntegralOrderList($data): array 
    {
        $_where = [
            ['integral_order.is_del', '=', 0]
        ];
        $data['keywords'] && $_where[] = ['integral_goods.title', 'like', "%{$data['keywords']}%"];
        return $this->_integralOrderList($data, $_where);
    }

    /*获取某个积分订单详情*/
    protected function _integralOrderInfo($_where): array 
    {
        $info = Order::leftJoin('integral_goods', 'integral_goods.id', '=', 'integral_order.goods_id')->leftJoin('integral_pick_up_goods','integral_pick_up_goods.order_id','=','integral_order.id')
            ->select('integral_order.*', 'integral_goods.title as goods_title','integral_pick_up_goods.pick_up_code','integral_pick_up_goods.name as pick_up_name','integral_pick_up_goods.phone as pick_up_phone','integral_pick_up_goods.address as pick_up_address','integral_pick_up_goods.remark as pick_up_remark','integral_pick_up_goods.is_pick','integral_pick_up_goods.pick_up_time','integral_pick_up_goods.logistics_company_code','integral_pick_up_goods.logistics_company_name','integral_pick_up_goods.waybill_number')
            ->where($_where)
            ->first()
            ->toArray();
        if(!$info){ return verify_err(null, '未找到该积分订单'); }
        if($info['freeze_status'] == 1){
            if($info['transfer_status'] == 1){
                $info['status_text'] = $info['is_pick'] == 1 ? '已收货' : '已发货';
            }else{
                $info['status_text'] = '待发货';
            }
        }else{
            $info['status_text'] = '未知状态';
        }
        return auto_return($info);
    }

    /*获取我的某个积分订单详情*/
   /* public function integralOrderInfo($data): array 
    {
        $_where['order.user_id'] = $data['user_id'];
        $_where['order.id'] = $data['id'];
        $_where['order.is_del'] = 0;
        return $this->_integralOrderInfo($_where);
    }*/
    /*后台获取某个积分订单的详情*/
    public function adminIntegralOrderInfo($data): array 
    {
         $_where = [
            ['integral_order.is_del', '=', 0],
            ['integral_order.id', '=', $data['id']]
        ];
        return $this->_integralOrderInfo($_where);
    }

    /*后台对某个订单进行发货操作(仅修改transfer_status字段)  在这之前先进行操作用户积分 解冻用户积分 + 用户转账平台积分*/
    public function shipmentsGoods($data): array 
    {
        $saveData['shipments_time'] = NOW;
        $saveData['transfer_status'] = 1;
        $savePickData['logistics_company_code'] = $data['logistics_company_code'];
        $savePickData['logistics_company_name'] = $data['logistics_company_name'];
        $savePickData['waybill_number'] = $data['waybill_number'];
        $res = Order::where([['id','=',$data['id']],['is_del','=',0],['freeze_status','=',1],['transfer_status','=',0]])->update($saveData);
        $pickRes = PickUpGoods::where([['order_id','=',$data['id']],['is_pick','=',0]])->update($savePickData);
        if($res && $pickRes){
            return auto_success($res, '发货成功');
        }
        return auto_failure(null, '发货失败');
    }

    /*我的积分订单 - 确认收货*/
   /* public function affirmPick($data): array 
    {
        $_where['p.user_id'] = $data['user_id'];
        $_where['p.order_id'] = $data['order_id'];
        $_where['p.is_pick'] = 0;
        $_where['p.status'] = 1;
        $_where['o.transfer_status'] = 1;
        $saveData['p.is_pick'] = 1;
        $saveData['p.pick_up_time'] = NOW;
        $res = $this->model($this->pickUpGoodsModel)
                    ->alias('p')
                    ->join('order o', 'o.id = p.order_id', 'LEFT')
                    ->where($_where)
                    ->update($saveData);
        return auto_return($res, '成功确认收货', '确认收货失败');
    }*/

    

}